﻿using AutoMapper;
using MediatR;
using SuYuanSystem.Applications;
using SuYuanSystem.Domain;
using SuYuanSystem.Repositories;
using SuYuanSystem.WebApi.MedialRs.Commands.Slaughters;

namespace SuYuanSystem.WebApi.MedialRs.CommandHandlers.Slaughters
{
    public class SlaughterCommandsHandlers : IRequestHandler<SlaughterCommands, ApiResult<object>>
    {
        public SlaughterCommandsHandlers(IRepository<Slaughter> repository, IMapper mapper)
        {
            Repository = repository;
            Mapper = mapper;
        }

        public IRepository<Slaughter> Repository { get; }
        public IMapper Mapper { get; }

        public Task<ApiResult<object>> Handle(SlaughterCommands request, CancellationToken cancellationToken)
        {
            try
            {
                ApiResult<object> result = new ApiResult<object>();
                result.statusCode = StatusCode.Success;
                result.Message = "添加成功";
                var entity = Mapper.Map<Slaughter>(request);
                int i = Repository.Insert(entity).Result;
                if (i <= 0)
                {
                    result.statusCode = StatusCode.FaieId;
                    result.Message = "添加失败";
                    return Task.FromResult(result);
                }
                return Task.FromResult(result);
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
